package com.book.service.decorator;

import com.book.pojo.Order;
import com.book.pojo.Product;
import com.book.repo.ProductRepository;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 具体实现类,用来实现用户积分更新的方法
 * @date 2024/4/25 22:02
 */
@Service
public class  OrderServiceDecorator extends AbstractOrderServiceDecorator{
    // 引入apollo配置的超时时间
    @Value("${dealy.service.time}")
    private String time;
    @Value("${service.level}")
    private String level;
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    protected void updateScoreAndSendRedPapers(String orderId, int serviceLvl, float price) {
        // todo 具体更新红包的逻辑
        switch (serviceLvl){
            case 0:
                int score = Math.round(price) /100;
                System.out.println("正常处理,为用户更新积分!score = "+ score);
                Optional<Product> product = productRepository.findById(Integer.valueOf(orderId));
                if (!product.isEmpty() && product.get().getSendRedBag().equals("1")) {
                    System.out.println("正常处理,为用户发放红包！product="+ product.get().getId());
                }

            case 1:

        }

    }

    public Order decoratorPay(String orderId, int serviceLvl, float price){
        // 调用原有的支付逻辑
        Order order = super.payOrder(orderId);
        // 新逻辑 发送红包，更新积分
        try{
            this.updateScoreAndSendRedPapers(orderId,serviceLvl,price);
        }catch (Exception e){
            // 重试机制，此处更新不能影响到主支付逻辑
        }
        return order;
    }
}
